package com.scs.application.modules.wm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.consts.SerialKeys;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.flow.FlowContext;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.wm.entity.OprtPrepare;
import com.scs.application.modules.wm.entity.OprtPrepareItem;
import com.scs.application.modules.wm.mapper.OprtPrepareItemMapper;
import com.scs.application.modules.wm.mapper.OprtPrepareMapper;
import com.scs.application.modules.wm.service.OprtPrepareItemService;
import com.scs.application.modules.wm.service.OprtPrepareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class OprtPrepareServiceImpl extends BaseServiceImpl<OprtPrepareMapper, OprtPrepare> implements OprtPrepareService {

    @Autowired
    private OprtPrepareItemMapper oprtPrepareItemMapper;

    @Autowired
    @Lazy
    private OprtPrepareItemService oprtPrepareItemService;

    @Override
    public boolean saveOrUpdate(OprtPrepare entity) {
        if (StringUtils.isBlank(entity.getPrepareKey())) {
            entity.setPrepareKey(serialRuleAdapter.getSerialRuleService().generateCode(SerialKeys.PREPARE_KEY));
        }
        return super.saveOrUpdate(entity);
    }
    @Override
    protected void beforeHandleFlowChange(FlowContext flowContext, OprtPrepare entity) {
        super.beforeHandleFlowChange(flowContext, entity);
        String buttonKey = flowContext.getButtonKey();
        // 提交审核，不允许空明细提交
        if ("submit".equalsIgnoreCase(buttonKey)) {
//            Integer count = oprtPrepareItemService.count(Wrappers.<OprtPrepareItem>query().eq("prepare_id", flowContext.getBusId()));
            List<OprtPrepareItem> oprtPrepareItems = oprtPrepareItemMapper.selectList(Wrappers.<OprtPrepareItem>query().eq("prepare_id", flowContext.getBusId()));
            if (oprtPrepareItems == null || oprtPrepareItems.isEmpty()) {
                throw new BusinessException("请添加明细后提交");
            } else {
                oprtPrepareItems.stream().filter(s -> "1".equals(s.getMatrType())).forEach(s -> {
                    List<OprtPrepareItem> prepareItems = oprtPrepareItemMapper.selectList(Wrappers.<OprtPrepareItem>query().eq("pack_prepare_item_id", s.getId()));
                    if (prepareItems == null || prepareItems.isEmpty()) {
                        throw new BusinessException("包内没有耗材");
                    }
                });
            }
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeCascadeByIds(Collection<? extends Serializable> idList) {
        super.removeCascadeByIds(idList);
        List<OprtPrepareItem> itemList = oprtPrepareItemService.list(Wrappers.<OprtPrepareItem>query().in("prepare_id",idList).select("id"));
        if (!itemList.isEmpty()) {
            oprtPrepareItemService.removeCascadeByIds(itemList.stream().map(OprtPrepareItem::getId).collect(Collectors.toList()));
        }
        return true;
    }
}
